package com.android.videotest.view.banner;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.android.videotest.view.loopviewpager.LoopViewPager;

import java.util.List;

/**
 * 项目名称：supermarket
 * 类 名 称：DSecondbannerV2
 * 类 描 述：副屏广告banner，在自定义View中获取数据，操作视图，直接贴在xml里
 * 创建时间：2019-11-05 16:01
 * 创 建 人：sansecy
 */
public class BannerView extends LoopViewPager {
    private static final String TAG = "DSecondBannerV2-App";
    public static final int DELAY_TIME = 5000;

    public BannerView(Context context) {
        super(context);
        initBannerView();
    }

    public BannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initBannerView();
    }

    private void initBannerView() {
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                switch (action) {
                    case MotionEvent.ACTION_DOWN:
                        stopAutoScroll();
                        break;
                    case MotionEvent.ACTION_UP:
                        startDelayAutoScroll();
                        break;
                }
                return false;
            }
        });
    }

    private boolean mIsAutoScroll = false;

    public void startAutoScroll() {
        if (!mIsAutoScroll) {
            Log.d(TAG, "startAutoScroll: ");
            post(mRunnable);
            mIsAutoScroll = true;
        }
    }

    public void startDelayAutoScroll() {
        if (!mIsAutoScroll) {
            Log.d(TAG, "startDelayAutoScroll: ");
            postDelayed(mRunnable, DELAY_TIME);
            mIsAutoScroll = true;
        }
    }

    public void stopAutoScroll() {
        if (mIsAutoScroll) {
            Log.d(TAG, "stopAutoScroll: ");
            removeCallbacks(mRunnable);
            mIsAutoScroll = false;
        }
    }

//    public void onDestroy() {
//        stopAutoScroll();
//        for (int i = 0; i < getChildCount(); i++) {
//            View child = getChildAt(i);
//            if (child instanceof HtdSimpleVideoView) {
//                ((HtdSimpleVideoView) child).release();
//            }
//        }
//    }

    private int mCurrentPosition = 0;

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
//            int count = mAdapter.getCount();
//            if (mCurrentPosition == count) {
//                mCurrentPosition = 0;
//            }
//            setCurrentItem(mCurrentPosition);
//            mCurrentPosition = mCurrentPosition % count;
//            mCurrentPosition++;
            PagerAdapter adapter = getAdapter();
            if (adapter == null) {
                return;
            }
            if (adapter.getCount() > 1 && mIsAutoScroll) {
                int currentItem = getCurrentItem() + 1;
                setCurrentItem(currentItem);
                postDelayed(mRunnable, DELAY_TIME);
            }
        }
    };

    public static abstract class AbsPagerAdapter<T> extends PagerAdapter {

        private final LoopViewPager mViewPager;
        private List<T> mBannerItems;

        public AbsPagerAdapter(LoopViewPager viewPager) {
            mViewPager = viewPager;
        }

        @Override
        public int getCount() {
            return mBannerItems == null ? 0 : mBannerItems.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(@NonNull ViewGroup view, int position, @NonNull Object object) {
            destroyItemView(view, position, object);
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            return instantiateItemView(container, position);
        }

        protected abstract View instantiateItemView(ViewGroup container, int position);

        protected abstract void destroyItemView(ViewGroup view, int position, Object object);

        public void setData(List<T> bannerItems) {
            mBannerItems = bannerItems;
            notifyDataSetChanged();
            mViewPager.setAdapter(this);
        }

        public T getItem(int position) {
            return mBannerItems.get(position);
        }

        public List<T> getBannerItems() {
            return mBannerItems;
        }

    }
}
